//
//  ViewController.m
//  HomeWork_20
//
//  Created by 徐康 on 16/8/10.
//  Copyright © 2016年 KX. All rights reserved.
//

#import "ViewController.h"
#import "FindCar.h"
#import "Result.h"
#import "FindCar.h"
#import "Brand.h"
#import "Brandlist.h"
#import "CustomTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) FindCar *car;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
}
- (void)handleData{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"AllCar" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    NSDictionary *findCardic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    self.car = [FindCar modelWithDic:findCardic];
    NSLog(@"%ld",_car.result.brandlist.count) ;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.car.result.brandlist.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.car.result.brandlist[section].list.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    Brand *brand = self.car.result.brandlist[indexPath.section].list[indexPath.row];
    cell.label.text = brand.name;
    [cell.image sd_setImageWithURL:[NSURL URLWithString:brand.imgurl]];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.car.result.brandlist[section].letter;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
